﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using m.Channelvn.Cached.Common;
using EntityFramework;
using System.Configuration;
using System.Net;
using System.IO;
using m.Channelvn.Cached.Common;


namespace m.Website.Controls.Touch
{
    public partial class ProductDetail : System.Web.UI.UserControl
    {
        protected long productId = 0;
        private string strHead = "<a href=\"{0}\">{1}</a><span class=\"narrow\"></span><a href=\"{2}\" class=\"current\">{3}</a>";
        private string strImg = "<li><img src=\"{0}\" /></li>";
        private string shortUrl = string.Empty;
        private int IMG_WIDTH = 974, IMG_HEIGHT = 721;
        protected string sTitle = "";
        protected string sUrl = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                productId = Lib.QueryString.ProductID;
                shortUrl = Lib.QueryString.shortURL;

                if (productId > 0)
                {
                    Product pd = ProductsBO.GetProductDetail(productId);

                    //add coookie
                    string cookieName = "_HongThuy_Product_" + productId.ToString();
                    HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

                    if (cookie == null)
                    {
                        try
                        {
                            int countVisit = ProductsBO.GetCountVisit(productId);
                            if (countVisit > 0)
                            {
                                cookie = new HttpCookie(cookieName);
                                cookie.Values["ProductID"] = productId.ToString();
                                cookie.Expires = DateTime.Now.AddHours(24);
                                HttpContext.Current.Response.Cookies.Add(cookie);
                            }

                        }
                        catch
                        {
                        }
                    }

                    ltrCategory.Text = string.Format(strHead,
                                                        "/" + shortUrl + ".htm",
                                                        ZoneProductBO.GetZoneProductNameByShortUrl(shortUrl),
                                                        "javascript:void(0);",
                                                        pd.Title);

                    if (pd.Avatar.Contains(","))
                    {
                        string[] arr = pd.Avatar.Split(',');
                        if (arr.Length > 0)
                        {
                            for (int i = 0; i < arr.Length; i++)
                            {
                                if (arr[i] != "")
                                {
                                    ltrMainImg.Text += "<img src=\"" + BO.Utils.CropImageMobile(300, 290, arr[i]) + "\" data-zoom-image=\"" + BO.Utils.CropImageMobile(IMG_WIDTH, IMG_HEIGHT, arr[i]) + "\"/>";
                                }
                                if (i == 3)
                                    break;
                            }
                        }
                    }

                    ltrTitle.Text = sTitle = pd.Title;
                    ltrType.Text = "<b>Mã sản phẩm: </b>" + pd.ProductCode;//;pd.FilterConditions;
                    ltrSapo.Text = pd.Sapo;
                    ltrExpire.Text = pd.Warranty;
                    sUrl = pd.Url;
                    ltrPrice.Text = pd.TypeSale == 1 ? "Giá: Liên hệ" : BO.Utils.ConvertMoneyNice(pd.price.ToString(), BO.Const.FormatMoney);
                    ltrDisCount.Text = pd.TypeSale == 1 ? "" : pd.sale == 0 ? "" : BO.Utils.ConvertMoneyNice(pd.sale.ToString(), BO.Const.FormatMoney);
                    //ltContent.Text = pd.longContent;
                    try
                    {
                        ltHangSX.Text = "Hãng sản xuất: " + BO.CompanyUserBO.CompanyUBO.GetInfomationUBO(pd.CompanyID.ToString()).Rows[0][BO.Const.tbCompanies.NameCp].ToString();
                    }
                    catch (Exception)
                    {

                    }

                    if (pd.NewsRelation != null && pd.NewsRelation != "")
                    {
                        rptData.DataSource = ProductsBO.GetRelationProduct(pd.NewsRelation.ToString());
                        rptData.DataBind();
                    }
                    else
                    {
                        pnSPLienQuan.Visible = false;
                    }

                    if (!String.IsNullOrEmpty(pd.Specifications))
                    {
                        rptSupport.DataSource = EmployeeBO.GetListSpecifications(pd.Specifications);
                        rptSupport.DataBind();
                    }
                }
            }
        }
    }
}